package com.hup.sokoban.view.mapDesigner;

import com.hup.sokoban.model.common.MapItem;
import com.hup.sokoban.service.mapDesigner.MapBoundsService;
import com.hup.sokoban.view.customView.MapLabel;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.swing.customView.ImageButton;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 处理地图边界和移动的面板
 *
 * @author hugan
 * @date 2020/5/6
 */
@Log4j2
public class MapBoundsPanel extends JPanel {
    public JPanel root;

    public ImageButton btnMoveUp;
    public ImageButton btnMoveRight;
    public ImageButton btnMoveLeft;
    public ImageButton btnMoveDown;
    public ImageButton btnRowAdd;
    public ImageButton btnRowDelete;
    public ImageButton btnColAdd;
    public ImageButton btnColDelete;

    private MapBoundsService mapItemDesignService;
    /**
     * 绑定被控制的 {@link MapLabel}
     */
    @Setter
    private MapLabel mapLabel;

    public MapBoundsPanel() {
        $$$setupUI$$$();
        mapItemDesignService = SpringContext.getBean(MapBoundsService.class);
        initListener();
    }

    private void initListener() {
        ActionListener al = e -> {
            if (mapLabel == null) {
                log.warn("未初始化mapLabel");
                return;
            }
            MapItem curMap = mapLabel.getMapItem();

            boolean hadChanged = false;
            Object source = e.getSource();
            if (source == btnMoveUp) {
                hadChanged = mapItemDesignService.moveUp(curMap);
            } else if (source == btnMoveDown) {
                hadChanged = mapItemDesignService.moveDown(curMap);
            } else if (source == btnMoveLeft) {
                hadChanged = mapItemDesignService.moveLeft(curMap);
            } else if (source == btnMoveRight) {
                hadChanged = mapItemDesignService.moveRight(curMap);
                //
            } else if (source == btnRowAdd) {
                hadChanged = mapItemDesignService.rowAdd(curMap);
            } else if (source == btnRowDelete) {
                hadChanged = mapItemDesignService.rowDelete(curMap);
            } else if (source == btnColAdd) {
                hadChanged = mapItemDesignService.colAdd(curMap);
            } else if (source == btnColDelete) {
                hadChanged = mapItemDesignService.colDelete(curMap);
            }

            if (hadChanged) {
                mapLabel.getMapItem().resetDesigningFields();
                mapLabel.notifyDataUpdated();
            }
        };
        btnMoveUp.setActionListener(al);
        btnMoveDown.setActionListener(al);
        btnMoveLeft.setActionListener(al);
        btnMoveRight.setActionListener(al);
        btnRowAdd.setActionListener(al);
        btnRowDelete.setActionListener(al);
        btnColAdd.setActionListener(al);
        btnColDelete.setActionListener(al);
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(panel1, gbc);
        btnMoveUp = new ImageButton();
        btnMoveUp.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/arrow_up.gif")));
        btnMoveUp.setPadding(2);
        btnMoveUp.setToolTipText("上移");
        btnMoveUp.setUnitedIconHeight(2.0f);
        btnMoveUp.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel1.add(btnMoveUp, gbc);
        btnMoveRight = new ImageButton();
        btnMoveRight.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/arrow_right.gif")));
        btnMoveRight.setPadding(2);
        btnMoveRight.setToolTipText("右移");
        btnMoveRight.setUnitedIconHeight(2.0f);
        btnMoveRight.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        panel1.add(btnMoveRight, gbc);
        btnMoveLeft = new ImageButton();
        btnMoveLeft.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/arrow_left.gif")));
        btnMoveLeft.setPadding(2);
        btnMoveLeft.setToolTipText("左移");
        btnMoveLeft.setUnitedIconHeight(2.0f);
        btnMoveLeft.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        panel1.add(btnMoveLeft, gbc);
        btnMoveDown = new ImageButton();
        btnMoveDown.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/arrow_down.gif")));
        btnMoveDown.setPadding(2);
        btnMoveDown.setToolTipText("下移");
        btnMoveDown.setUnitedIconHeight(2.0f);
        btnMoveDown.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        panel1.add(btnMoveDown, gbc);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(panel2, gbc);
        btnRowAdd = new ImageButton();
        btnRowAdd.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/row_add.gif")));
        btnRowAdd.setPadding(2);
        btnRowAdd.setToolTipText("插入行");
        btnRowAdd.setUnitedIconHeight(2.0f);
        btnRowAdd.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel2.add(btnRowAdd, gbc);
        btnRowDelete = new ImageButton();
        btnRowDelete.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/row_delete.gif")));
        btnRowDelete.setPadding(2);
        btnRowDelete.setToolTipText("删除行");
        btnRowDelete.setUnitedIconHeight(2.0f);
        btnRowDelete.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel2.add(btnRowDelete, gbc);
        btnColAdd = new ImageButton();
        btnColAdd.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/col_add.gif")));
        btnColAdd.setPadding(2);
        btnColAdd.setToolTipText("插入列");
        btnColAdd.setUnitedIconHeight(2.0f);
        btnColAdd.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel2.add(btnColAdd, gbc);
        btnColDelete = new ImageButton();
        btnColDelete.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/col_delete.gif")));
        btnColDelete.setPadding(2);
        btnColDelete.setToolTipText("删除列");
        btnColDelete.setUnitedIconHeight(2.0f);
        btnColDelete.setUnitedIconWidth(2.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        panel2.add(btnColDelete, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

}
